package com.jdew.thkj.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.utils.DialogUtils;

/**
 * Created by JYY on 2017/8/4.
 */

public class CommonDialog {

    public static class Builder {
        private Context mContext;
        public Dialog mDialog;
        private ViewHolder mViewHolder;
        private ViewHolder2 mViewHolder2;
        public static String url;
        private SharedPreferences sp;
        private SharedPreferences.Editor editor;
        private View mView;
        private boolean hasPos = false,
                hasNeg = false;

        public Builder(Activity context) {
            mContext = context;
            initView();
        }

        public Builder(Activity context, String message) {
            mContext = context;
            initView3();
        }

        public Builder(Activity context, Boolean ispay, int type) {
            mContext = context;
            sp = context.getSharedPreferences("user", Activity.MODE_PRIVATE);
            editor = sp.edit();
            initView4();
        }

        public Builder(Activity context, int i) {
            mContext = context;
            initView2();
        }

        public Builder(Context context, boolean update) {
            mContext = context;
            updateInitView();
        }
        public Builder(Context context,int i,int j) {
            mContext = context;
            toKenInitView();
        }

        public Builder setTitle(CharSequence title) {
            mViewHolder.tvTitle.setText(title);
            return this;
        }

        public Builder setTitle2(CharSequence title) {
            mViewHolder2.tvTitle.setText(title);
            return this;
        }

        public Builder setTitle(CharSequence title, int color) {
            mViewHolder.tvTitle.setText(title);
            mViewHolder.tvTitle.setTextColor(ContextCompat.getColor(mContext, color));
            return this;
        }

        public Builder setTitle(int resid) {
            mViewHolder.tvTitle.setText(resid);
            return this;
        }

        public Builder setTitle(int resid, int color) {
            mViewHolder.tvTitle.setText(resid);
            mViewHolder.tvTitle.setTextColor(ContextCompat.getColor(mContext, color));
            return this;
        }

        public Builder setMessage(CharSequence title) {
            mViewHolder.tvMessage.setText(title);
            return this;
        }

        public Builder setMessage(CharSequence title, int color) {
            mViewHolder.tvMessage.setText(title);
            mViewHolder.tvMessage.setTextColor(ContextCompat.getColor(mContext, color));
            return this;
        }

        public Builder setMessage(int resid) {
            mViewHolder.tvMessage.setText(resid);
            return this;
        }

        public Builder setMessage(int resid, int color) {
            mViewHolder.tvMessage.setText(resid);
            mViewHolder.tvMessage.setTextColor(ContextCompat.getColor(mContext, color));
            return this;
        }

        public Builder setMessageGravity() {
            mViewHolder.tvMessage.setGravity(Gravity.CENTER);
            return this;
        }

        public Builder setMessageGravity2() {
            mViewHolder2.tvMessage.setGravity(Gravity.CENTER);
            return this;
        }
        public Builder setMessageGravity3() {
            mViewHolder.tvMessage.setGravity(Gravity.LEFT);
            return this;
        }
        public String getPositiveText() {
            return mViewHolder.tvPositiveButton.getText().toString();
        }

        public Builder setPositiveButton(CharSequence text, final View.OnClickListener listener) {
            mViewHolder.tvPositiveButton.setVisibility(View.VISIBLE);
            if (null != mViewHolder.dialog2) {
                mViewHolder.dialog2.setVisibility(View.GONE);
            }
            hasPos = true;
            mViewHolder.tvPositiveButton.setText(text);
            mViewHolder.tvPositiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.cancel();
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }

        public Builder setPositiveButton(CharSequence text, final View.OnClickListener listener, int color) {
            mViewHolder.tvPositiveButton.setVisibility(View.VISIBLE);
            hasPos = true;
            mViewHolder.tvPositiveButton.setText(text);
            mViewHolder.tvPositiveButton.setTextColor(ContextCompat.getColor(mContext, color));
            mViewHolder.tvPositiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.cancel();
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }

        public Builder setNegativeButton(CharSequence text, final View.OnClickListener listener) {
            mViewHolder.tvNegativeButton.setVisibility(View.VISIBLE);
            hasNeg = true;
            mViewHolder.tvNegativeButton.setText(text);
            mViewHolder.tvNegativeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != mDialog && mDialog.isShowing()) {
                        mDialog.cancel();
                    }
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }

        public Builder setNegativeButton(final View.OnClickListener listener) {
            mViewHolder.update_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.cancel();
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }

        public Builder setNegativeButton2(CharSequence text, final View.OnClickListener listener) {
            mViewHolder2.tvNegativeButton.setVisibility(View.VISIBLE);
            hasNeg = true;
            mViewHolder2.tvNegativeButton.setText(text);
            mViewHolder2.tvNegativeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != mDialog && mDialog.isShowing()) {
                        mDialog.cancel();
                    }
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }

        public Builder setPositiveButton2(CharSequence text, final View.OnClickListener listener) {
            mViewHolder2.tvPositiveButton.setVisibility(View.VISIBLE);
            if (null != mViewHolder2.dialog2) {
                mViewHolder2.dialog2.setVisibility(View.GONE);
            }
            hasPos = true;
            mViewHolder2.tvPositiveButton.setText(text);
            mViewHolder2.tvPositiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.cancel();
                    editor.putString("newUserCode", mViewHolder2.tvMessage.getText().toString().trim());
                    editor.apply();
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }

        public Builder setdialogPostive(CharSequence text, final View.OnClickListener listener) {
            hasNeg = true;
            if (null != mViewHolder.dialog2) {
                mViewHolder.dialog2.setVisibility(View.VISIBLE);
            }
            mViewHolder.sure.setText(text);
            mViewHolder.sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.cancel();
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }

        public Builder setdialogPostive(CharSequence text, int i, final View.OnClickListener listener) {
            hasNeg = true;
            if (null != mViewHolder.dialog2) {
                mViewHolder.dialog2.setVisibility(View.GONE);
            }
            mViewHolder.tvPositiveButton.setText(text);
            mViewHolder.tvPositiveButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mDialog.cancel();
                    if (listener != null) {
                        listener.onClick(view);
                    }
                }
            });
            return this;
        }

        public Builder setCancelable(boolean flag) {
            mDialog.setCancelable(flag);
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean flag) {
            mDialog.setCanceledOnTouchOutside(flag);
            return this;
        }

        public Dialog create() {
            return mDialog;
        }

        public void show() {
            if (mDialog != null) {
                if (hasPos || hasNeg) {
                    mViewHolder.line1.setVisibility(View.VISIBLE);
                }
                if (hasPos && hasNeg) {
                    mViewHolder.line2.setVisibility(View.VISIBLE);
                }
                mDialog.show();
            }
        }

        public void show3() {
            if (mDialog != null) {
                if (hasPos || hasNeg) {
                    mViewHolder.line1.setVisibility(View.GONE);
                }
                if (hasPos && hasNeg) {
                    mViewHolder.line2.setVisibility(View.GONE);
                }
                mDialog.show();
            }
        }
        public void show4() {
            if (mDialog != null) {
                if (hasPos || hasNeg) {
                    mViewHolder.line1.setVisibility(View.GONE);
                }
                if (hasPos && hasNeg) {
                    mViewHolder.line2.setVisibility(View.GONE);
                }
                mViewHolder.tvNegativeButton.setVisibility(View.GONE);
                mDialog.show();
            }
        }

        public void show2() {
            if (mDialog != null) {
                if (hasPos || hasNeg) {
                    mViewHolder2.line1.setVisibility(View.GONE);
                }
                if (hasPos && hasNeg) {
                    mViewHolder2.line2.setVisibility(View.GONE);
                }
                mDialog.show();
            }
        }

        public void dismiss() {
            if (mDialog != null) {
                mDialog.cancel();
            }
        }

        private void initView() {
            mDialog = new Dialog(mContext, DialogUtils.getStyle());
            mView = LayoutInflater.from(mContext).inflate(R.layout.layout_easy_dialog, null);
            mViewHolder = new ViewHolder(mView);
            mDialog.setContentView(mView);

            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = (int) (dm.widthPixels * 0.8);
            mDialog.getWindow().setAttributes(lp);
        }

        private void initView2() {
            if (null != mContext && null != mDialog) {
                mDialog.cancel();
            }
            mDialog = new Dialog(mContext, DialogUtils.getStyle());
            mView = LayoutInflater.from(mContext).inflate(R.layout.layout_easy_dialog2, null);
            mViewHolder = new ViewHolder(mView);
            mDialog.setContentView(mView);

            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = (int) (dm.widthPixels * 0.8);
            mDialog.getWindow().setAttributes(lp);
        }

        private void initView3() {
            mDialog = new Dialog(mContext, DialogUtils.getStyle());
            mView = LayoutInflater.from(mContext).inflate(R.layout.layout_easy_dialog3, null);
            mViewHolder = new ViewHolder(mView);
            mDialog.setContentView(mView);

            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = (int) (dm.widthPixels * 0.8);
            mDialog.getWindow().setAttributes(lp);
        }

        private void initView4() {
            mDialog = new Dialog(mContext, DialogUtils.getStyle());
            mView = LayoutInflater.from(mContext).inflate(R.layout.layout_easy_dialog4, null);
            mViewHolder2 = new ViewHolder2(mView);
            mDialog.setContentView(mView);

            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = (int) (dm.widthPixels * 0.8);
            mDialog.getWindow().setAttributes(lp);
        }

        private void updateInitView() {
            if (null != mContext && null != mDialog) {
                mDialog.cancel();
            }
            mDialog = new Dialog(mContext, DialogUtils.getStyle());
            mView = LayoutInflater.from(mContext).inflate(R.layout.layout_easy_update, null);
            mViewHolder = new ViewHolder(mView);
            mDialog.setContentView(mView);

            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = (int) (dm.widthPixels * 0.8);
            mDialog.getWindow().setAttributes(lp);
        }
        private void toKenInitView() {
            if (null != mContext && null != mDialog) {
                mDialog.cancel();
            }
            mDialog = new Dialog(mContext, DialogUtils.getStyle());
            mView = LayoutInflater.from(mContext).inflate(R.layout.layout_easy_token, null);
            mViewHolder = new ViewHolder(mView);
            mDialog.setContentView(mView);

            DisplayMetrics dm = new DisplayMetrics();
            WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            windowManager.getDefaultDisplay().getMetrics(dm);
            WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes();
            lp.width = (int) (dm.widthPixels * 0.8);
            mDialog.getWindow().setAttributes(lp);
        }

        public static class ViewHolder {

            TextView tvTitle;
            TextView tvMessage;
            TextView tvPositiveButton, tvNegativeButton, sure;
            ImageView update_cancle;
            LinearLayout vgLayout, dialog1, dialog2;
            View line1, line2;

            public ViewHolder(View view) {

                tvTitle = (TextView) view.findViewById(R.id.dialog_title);
                tvMessage = (TextView) view.findViewById(R.id.dialog_message);
                update_cancle = (ImageView) view.findViewById(R.id.update_cancle);
                tvPositiveButton = (TextView) view.findViewById(R.id.dialog_positive);
                tvNegativeButton = (TextView) view.findViewById(R.id.dialog_negative);
                sure = (TextView) view.findViewById(R.id.sure);
                vgLayout = (LinearLayout) view.findViewById(R.id.dialog_layout);
                dialog1 = (LinearLayout) view.findViewById(R.id.dialog1);
                dialog2 = (LinearLayout) view.findViewById(R.id.dialog2);
                vgLayout = (LinearLayout) view.findViewById(R.id.dialog_layout);
                line1 = view.findViewById(R.id.dialog_line1);
                line2 = view.findViewById(R.id.dialog_line2);
                url = tvMessage.getText().toString();
            }
        }

        public static class ViewHolder2 {

            TextView tvTitle;
            EditText tvMessage;
            TextView tvPositiveButton, tvNegativeButton, sure;
            ImageView update_cancle;
            LinearLayout vgLayout, dialog1, dialog2;
            View line1, line2;

            public ViewHolder2(View view) {

                tvTitle = (TextView) view.findViewById(R.id.dialog_title);
                tvMessage = (EditText) view.findViewById(R.id.dialog_message);
                update_cancle = (ImageView) view.findViewById(R.id.update_cancle);
                tvPositiveButton = (TextView) view.findViewById(R.id.dialog_positive);
                tvNegativeButton = (TextView) view.findViewById(R.id.dialog_negative);
                sure = (TextView) view.findViewById(R.id.sure);
                vgLayout = (LinearLayout) view.findViewById(R.id.dialog_layout);
                dialog1 = (LinearLayout) view.findViewById(R.id.dialog1);
                dialog2 = (LinearLayout) view.findViewById(R.id.dialog2);
                vgLayout = (LinearLayout) view.findViewById(R.id.dialog_layout);
                line1 = view.findViewById(R.id.dialog_line1);
                line2 = view.findViewById(R.id.dialog_line2);
                url = tvMessage.getText().toString();
            }
        }

    }

}
